package com.sfpay.merchant.auth.controller.index;

import com.sfpay.merchant.auth.entity.system.SysMenu;
import com.sfpay.merchant.auth.entity.system.SysUser;
import com.sfpay.merchant.auth.service.ISysConfigService;
import com.sfpay.merchant.auth.service.ISysMenuService;
import com.sfpay.merchant.auth.shiro.ShiroUtils;
import com.sfpay.merchant.auth.shiro.service.SysPasswordService;
import com.sfpay.merchant.common.config.SFPayConfig;
import com.sfpay.merchant.common.constant.ShiroConstants;
import com.sfpay.merchant.common.core.controller.BaseController;
import com.sfpay.merchant.common.core.domain.AjaxResult;
import com.sfpay.merchant.common.core.text.Convert;
import com.sfpay.merchant.common.utils.CookieUtils;
import com.sfpay.merchant.common.utils.ServletUtils;
import com.sfpay.merchant.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.sfpay.merchant.auth.shiro.ShiroUtils.getSysUser;

/**
 * @describe: 首页 业务处理
 * @author:01410843
 * @Date 2022/8/12 14:18
 **/
@Controller
@Slf4j
public class SysIndexController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService iSysConfigService;

    @Autowired
    private SFPayConfig sfPayConfig;

    @Resource
    private SysPasswordService sysPasswordService;

    /**
     * @return java.lang.String
     * @Author 01410843
     * @Description 系统首页
     * @Date 14:22 2022/8/12
     * @Param [mmap]
     **/
    @GetMapping("/index")
    public String index(ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", iSysConfigService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", iSysConfigService.selectConfigByKey("sys.index.skinName"));
        Boolean footer = Convert.toBool(iSysConfigService.selectConfigByKey("sys.index.footer"), true);
        Boolean tagsView = Convert.toBool(iSysConfigService.selectConfigByKey("sys.index.tagsView"), true);
        mmap.put("footer", footer);
        mmap.put("tagsView", tagsView);
        mmap.put("mainClass", contentMainClass(footer, tagsView));
        mmap.put("copyrightYear", sfPayConfig.getCopyrightYear());
        mmap.put("demoEnabled", sfPayConfig.isDemoEnabled());
        mmap.put("isMobile", ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")));

        // 菜单导航显示风格
        String menuStyle = iSysConfigService.selectConfigByKey("sys.index.menuStyle");
        // 移动端，默认使左侧导航菜单，否则取默认配置
        String indexStyle = ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;
        // 优先Cookie配置导航菜单
        Cookie[] cookies = ServletUtils.getRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (StringUtils.isNotEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName())) {
                indexStyle = cookie.getValue();
                break;
            }
        }
        String webIndex = "topnav".equalsIgnoreCase(indexStyle) ? "index-topnav" : "index";
        return webIndex;
    }

    // 系统主页面
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        mmap.put("version", sfPayConfig.getVersion());
        return "main_v1";
    }

    // 锁定屏幕
    @GetMapping("/lockscreen")
    public String lockscreen(ModelMap mmap) {
        mmap.put("user", ShiroUtils.getSysUser());
        ServletUtils.getSession().setAttribute(ShiroConstants.LOCK_SCREEN, true);
        return "lock";
    }

    // 解锁屏幕
    @PostMapping("/unlockscreen")
    @ResponseBody
    public AjaxResult unlockscreen(String password) {
        SysUser user = ShiroUtils.getSysUser();
        if (StringUtils.isNull(user)) {
            return AjaxResult.error("服务器超时，请重新登录");
        }
        if (sysPasswordService.matches(user, password)) {
            ServletUtils.getSession().removeAttribute(ShiroConstants.LOCK_SCREEN);
            return AjaxResult.success();
        }
        return AjaxResult.error("密码不正确，请重新输入。");
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin() {
        return "skin";
    }

    // 切换菜单
    @GetMapping("/system/menuStyle/{style}")
    public void menuStyle(@PathVariable String style, HttpServletResponse response) {
        CookieUtils.setCookie(response, "nav-style", style);
    }

    /**
     * @return java.util.List<com.sfpay.merchant.auth.entity.system.SysMenu>
     * @Author 01410843
     * @Description 获取菜单列表
     * @Date 18:40 2022/8/12
     * @Param []
     **/
    @GetMapping("/getMenus")
    @ResponseBody
    public List<SysMenu> getMenus() {
        SysUser user = getSysUser();
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        return menus;
    }

    public String contentMainClass(Boolean footer, Boolean tagsView) {
        if (!footer && !tagsView) {
            return "tagsview-footer-hide";
        } else if (!footer) {
            return "footer-hide";
        } else if (!tagsView) {
            return "tagsview-hide";
        }
        return StringUtils.EMPTY;
    }
}
